﻿using AppServices.User;
using AppServices.User.Dto;
using Domain.Entities;
using Infrastructure.Models;
using Infrastructure.Models.Others;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using static AppServices.User.Dto.UserInfoInput;

namespace WorldSystem.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly ILogger<UsersController> _logger;
        private readonly UserInfoAppService _userInfoService;

        public UsersController(ILogger<UsersController> logger, UserInfoAppService userInfoService)
        {
            this._logger = logger;
            this._userInfoService = userInfoService;
        }

        /// <summary>
        /// 登陆接口 api/users/login
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [AllowAnonymous]
        public async Task<ApiResult> Login(LoginReq loginReq)
        {
            try
            {
                string token = await this._userInfoService.Login(loginReq);
                return ApiResult.Success(token);
            }
            catch (Exception ex)
            {
                return ApiResult.Error(ex.InnerException?.Message?? ex.Message);
            }
        }

        ///// <summary>
        ///// 注册接口 api/users/register
        ///// </summary>
        ///// <param name="loginDto"></param>
        ///// <returns></returns>
        //[HttpPost("[action]")]
        //public async Task<ApiResult<UserInfoDto>> Register(UserInfoInput userInput)
        //{
        //    UserInfoDto userInfo = await _userInfoService.AddUser(userInput);
        //    return ApiResult<UserInfoDto>.Success(userInfo);          
        //}

        /// <summary>
        /// 获取用户列表接口 api/users?pageSize=10&page=1&keyword=张三&sort=0&oderBy=name
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<UserInfo>>> Get()
        {
            List<UserInfo> userInfos = await _userInfoService.Query();
            return ApiResult<List<UserInfo>>.Success(userInfos);
            
        }

        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<UsersController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }
        // PUT api/<UsersController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<UsersController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
